import { Group } from "zrender";
import { CoordinateSystem } from "./CoordinateSystem";

export interface CsElementOption {
  show: boolean
  z: number
}

export abstract class CsElement<T extends CsElementOption = CsElementOption> {
  protected _option: T
  readonly root = new Group()
  readonly map: CoordinateSystem;

  get show() {
    return !this.root.ignore
  }

  set show(val) {
    if (val != this.show) {
      if (val) {
        this.root.show()
      } else {
        this.root.hide()
      }
    }
  }

  constructor(map: CoordinateSystem, option: T) {
    this.map = map
    this._option = option
    map.root.add(this.root)
  }

  destroy() {
    const { map, root } = this
    map.root.remove(root)
    this._onDestroy()
  }

  protected abstract _onDestroy(): void
}